<template>
    <div class="container">
        <el-form ref="form" :model="form" label-width="120px" size="mini" v-loading="saveLoding">
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item label="纳税人名称">
                        <el-input v-model="form.qymc" suffix-icon="el-icon-search"></el-input>

                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="纳税人识别号">
                        <el-input v-model="form.tyshxydm"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="24">
                    <el-form-item label="数据年度">
                        <!-- <el-input v-model="form.sjnd" maxlength='4' type="number"></el-input> -->
                        <el-date-picker v-model="form.sjnd" value-format="yyyy" type="year" placeholder="选择年">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="数据列表">
                        <div class="head_tb">
                            <div>
                                <el-button type="danger" size="mini" class="mybtn" @click="del()"><i class="el-icon-minus"></i></el-button>
                                <el-button type="danger" size="mini" class="mybtn" @click="upload()"><i class="el-icon-plus"></i></el-button>
                            </div>
                            <div class="title_tb">共 {{tableDatas.length}} 个</div>

                        </div>
                        <el-table v-loading="loading" ref="bqEltable" :data="tableDatas" style="width: 100%" fit border @selection-change="dochandleSelectionChange">
                            <el-table-column type="selection" width="63" align="center"> </el-table-column>
                            <el-table-column type="index" label="序号" width="60" align="center" :index="currIndex"> </el-table-column>
                            <el-table-column label="文件名称" prop="wjmc" align="center">
                            </el-table-column>
                            <el-table-column label="数据量" prop="sjl" align="center">
                            </el-table-column>
                        </el-table>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="说明">
                        <el-input type="textarea" v-model="form.illustrate" :autosize="{ minRows: 4, maxRows: 8 }"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="20">
                <el-col :span="12"> <el-form-item label="上传时间">
                        <el-input v-model="form.scsj" disabled></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="上传人">
                        <el-input v-model="form.scry" disabled></el-input>
                    </el-form-item>
                </el-col>

            </el-row>

            <el-form-item>
                <div style="text-align:center;">
                    <el-button type="primary" @click="onSubmit">保存</el-button>
                    <el-button @click="go('list')">返回</el-button>
                </div>
            </el-form-item>
        </el-form>
        <!-- 上传组件隐藏 -->
        <el-upload style="display:none;" ref="uploadTemplate" class="upload-demo" :before-upload="beforeUpload" action drag :show-file-list="false" :http-request="httpRequest" :multiple="false" accept=".xls,.csv,.xlsx">
            <el-button size="small" type="primary" ref="uploadTemplate_btn">点击上传</el-button>
        </el-upload>
    </div>
</template>

<script>
import { add } from '@/api/zcgj/yzgj'
export default {

    data() {
        return {
            saveLoding: false,
            form: {
                qymc: '',
                tyshxydm: '',
                illustrate: '',
                sjnd: '',
                scsj: '',
                scry: '',

            },

            files: [],

            rwform: [],
            loading: false,
            tableDatas: [],
            docSelect: []

        }
    },
    methods: {
        onSubmit() {
            const reg = /^[0-9]+$/;

            if (this.form.qymc == '') {
                this.$message.error('请输入企业名称')
                return false
            }
            if (this.form.tyshxydm == '') {
                this.$message.error('请输入社会信用代码')
                return false
            }
            if (this.form.sjnd == '') {
                this.$message.error('请输入所属年度')
                return false
            } else if (!reg.test(this.form.sjnd)) {
                this.$message.error('数据年度请输入0-9之间的数字')
                return false
            }

            if (this.files.length <= 0) {
                this.$message.error('请上传文件')
                return false
            }

            let fd = new FormData();
            fd.append("sjnd", this.form.sjnd);
            fd.append("tyshxydm", this.form.tyshxydm);
            fd.append("qymc", this.form.qymc);
            fd.append("illustrate", this.form.illustrate);
            for (let i = 0; i < this.files.length; i++) {

                fd.append("files", this.files[i].file, this.files[i].file.name);


            }
            this.save(fd)

        },

        save(data) {
            this.saveLoding = true
            add(data).then((res) => {
                this.saveLoding = false
                if (res.code == 200) {

                    this.$message({
                        type: "success",
                        message: '保存成功',
                    });
                    // this.getTableData()
                    this.go('list')
                }
            });
            
        },
        del() {
            if (this.docSelect.length > 0) {
                this.loading = true;
                this.$confirm('该删除后将不可恢复，是否删除？', '友情提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    cancelButtonClass: 'JmconfirmCancelButton',
                    type: 'warning'
                }).then(() => {
                    for (let i = 0; i < this.docSelect.length; i++) {
                        for (let j = 0; j < this.tableDatas.length; j++) {
                            if (this.docSelect[i].wjmc == this.tableDatas[j].wjmc) {
                                this.tableDatas.splice(j, 1)
                                this.files.splice(j, 1)
                            }
                        }
                    }

                    this.loading = false

                }).catch(error => {
                    this.loading = false;
                })

            } else {
                this.$message.error('请选择一条数据删除');
                return false;
            }
        },
        //s上传名册
        upload() {
            this.$refs['uploadTemplate'].$refs['upload-inner'].handleClick()
        },
        beforeUpload(file, fileList) {
            // console.log('文件状态改变前',file);
            // if(!file.name.includes('.pdf')) {
            //     this.$message.error('请上传pdf格式文件')
            //     return false
            // }

            // if (file.size > 1024 * 1024 * 10) {
            //     this.$message.warning(
            //         "你上传的 \“" + file.name + "\” 文件过大，请上传小于2M的文件。"
            //     );
            //     return false;
            // }

            if (
                file.name.indexOf("@") != -1 ||
                file.name.indexOf("#") != -1 ||
                file.name.indexOf('""') != -1 ||
                file.name.indexOf("<") != -1 ||
                file.name.indexOf(">") != -1 ||
                file.name.indexOf("?") != -1 ||
                file.name.indexOf("|") != -1 ||
                file.name.indexOf("/") != -1 ||
                file.name.indexOf(":") != -1 ||
                file.name.indexOf("`\`") != -1
            ) {
                this.$message.warning(
                    "你上传的 \“" +
                    file.name +
                    "\” 文件有特殊字符，文件名中不可存在特殊字符,请重新上传"
                );
                return false;
            }

            let index = file.name.lastIndexOf(".");
            let Type = file.name.substr(index + 1, file.name.length);
            if (["xls", "csv", "xlsx"].indexOf(Type.toLowerCase()) === -1) {
                this.$message({
                    type: "warning",
                    message: "请上传后缀名为xls,csv,xlsx的附件！",
                });
                return false;
            }
        },
        httpRequest(data) {
            this.tableDatas.push({
                wjmc: data.file.name.split('.x')[0]
            })
            this.files.push(data)

        },
        currIndex(index) {
            return index + 1;
        },
        rwHandle() {
            this.dialogVisible = true
        },
        dochandleSelectionChange(selectedRows) {
            this.docSelect = selectedRows;
        },
        //待处理
        go(name, params) {
            this.$emit('changePage', name, params);
        },
    }
}
</script>

<style lang="scss" scoped>
.container {
    height: 100%;
    width: 100%;
    background-color: #fff;
    padding: 15px 50px;

    .btn {
        padding: 8px 8px 8px 0;
    }

    .el-select,
    .el-input,
    .el-date-editor {
        width: 100%;
    }

    .imgIcon {
        width: 30px;
        height: 30px;

        img {
            width: 100%;
            height: 100%;
        }
    }

    .codeC {
        display: flex;
        justify-content: space-between;

        .tabs {
            width: 35px;

            .tabs-item {
                text-align: center;
                padding: 11px;
                height: 80px;
                background-color: rgba(22, 155, 213, 1);
                box-sizing: border-box;

                border-radius: 5px;
                -moz-box-shadow: none;
                -webkit-box-shadow: none;
                box-shadow: none;
                font-size: 14px;
                color: #ffffff;
                border-radius: 5px;
            }
        }
    }

    .timeSelct {
        .el-input {
            display: inline;
        }
    }

    .el-dialog .el-dialog__body .el-form .el-select {
        display: inline !important;
    }

    ::v-deep .el-dialog .el-dialog__body .el-form .el-form-item__content {
        width: calc(100% - 100px);
    }
    .mybtn {
        width: 24px;
        height: 24px;
        border-radius: 50%;
        padding: 0;
    }

    .head_tb {
        display: flex;
        justify-content: space-between;
        margin-bottom: 10px;

        .title_tb {
            // border-left:4px solid #ccc;
        }
    }
}
</style>